package com.queclink.ycm.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.entity.data.Data;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-21 16:31:00
 */
public class Record {

    private Integer RecordLength;
    private String GeneratedTime;
    private String RecordID;
    private String EventCode;
    private List<Data> DataIDs;
    private boolean Appended;

    public Record() {
    }

    @JSONField(
            name = "Appended"
    )
    public boolean isAppended() {
        return this.Appended;
    }

    public void setAppended(boolean appended) {
        this.Appended = appended;
    }

    @JSONField(
            name = "RecordLength"
    )
    public Integer getRecordLength() {
        return this.RecordLength;
    }

    public void setRecordLength(Integer recordLength) {
        this.RecordLength = recordLength;
    }

    @JSONField(
            name = "GeneratedTime"
    )
    public String getGeneratedTime(JSONObject jsonObject) {
        String generatedTime = jsonObject.getString("GeneratedTime");
        return HexUtils.LongStrToHex(generatedTime, 8);
    }

    @JSONField(
            name = "RecordCountNumber"
    )
    public String getRecordCountNumber(JSONObject jsonObject) {
        Integer recordCountNumber = jsonObject.getInteger("RecordCountNumber");
        return HexUtils.intToHex(recordCountNumber, 4);
    }

    public void setGeneratedTime(String generatedTime) {
        this.GeneratedTime = generatedTime;
    }

    @JSONField(
            name = "ID"
    )
    public String getRecordID(JSONObject jsonObject) {
        String s = jsonObject.getString("ID");
        StringBuffer sb = new StringBuffer(s);
        if (sb.length() < 2) {
            sb.insert(0, "0");
        }
        return sb.toString();
    }

    public void setRecordID(String RecordID) {
    }

    @JSONField(
            name = "EventCode"
    )
    public String getEventCode() {
        return "00";
    }

    public void setEventCode(String eventCode) {
        this.EventCode = eventCode;
    }

    @JSONField(
            name = "DataIDs"
    )
    public List<Data> getDataIDs() {
        return this.DataIDs;
    }

    public void setDataIDs(List<Data> dataIDs) {
        this.DataIDs = dataIDs;
    }

    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getGeneratedTime(jsonObject));
        sb.append(this.getRecordCountNumber(jsonObject));
        sb.append(this.getRecordID(jsonObject));
        sb.append(this.getEventCode());
        JSONArray dataIDs = jsonObject.getJSONArray("DataIDs");
        for (Object dataID : dataIDs) {
            JSONObject dataIDJsonObject = JSONObject.parseObject(dataID.toString());
            Class<?> c = Class.forName("com.queclink.ycm.entity.data.Data" + dataIDJsonObject.getString("ID"));
            Data data = (Data) c.newInstance();
            JSONObject dataJsonObject = dataIDJsonObject.getJSONObject("Data");
            String result = data.combine(dataJsonObject);
            sb.append(result);
        }

        int len = sb.length() + 2;
        len = len / 2;
        if (len > 127) {
            len++;
//            sb.insert(0, "8");
            sb.insert(0, HexUtils.intToHex(len, 3));
            sb.insert(0, "8");
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        return sb.toString();
    }
}
